<?php

namespace App\Http\Controllers\Customer;

use App\Http\Controllers\Controller;
use App\Models\AddressModel;
use App\Models\airline\base\RouteModel;
use App\Models\AirportModel;
use App\Models\base\CurrencyModel;
use App\Models\CountryModel;
use Illuminate\Http\Request;

//国家地址查询
class Country extends Controller
{

    public function index(Request $request){
        $pid = intval($request->input('pid'));
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $query = CountryModel::query();
//        $query = AddressModel::query();

        if($pid) {
            $query->where('pid', $pid);
        } else {
            $query->where('pid', 7);
        }

        $total = $query->count('id');
        $list = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->get();
        return $this->success(['list' => $list, 'total' => $total]);
    }

    public function country(Request $request){
        $pid = intval($request->input('pid'));
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $query = CountryModel::query();

        if($pid) {
            $query->where('pid', $pid);
        } else {
            $query->where('level', 2);
        }

        $total = $query->count('id');
        $list = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->get();
        return $this->success(['list' => $list, 'total' => $total]);
    }

    //airport
    public function airports(Request $request){
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $items = AirportModel::query()
            ->select('id', 'iata', 'country_name', 'name', 'city')
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->get();
        return $this->success($items);
    }

    //目的地
    public function destination(){
        $route = (new RouteModel)->get();

        $list = $route->map(function($item){
            return [
                'name' => $item->arrive_port_code,
            ];
        })->unique('name');

        return $this->success($list);

    }

    //币种
    public function currency(){
        $currency = (new CurrencyModel())->get();

        $list = $currency->map(function($item){
            return [
                'id' => $item->id,
                'name' => $item->short_code,
            ];
        });

        return $this->success($list);
    }

}
